package com.yiwei.hdb.api.requestInfo;

import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.UpdateAddressBean;
import com.yiwei.hdb.api.callback.AddressContract;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class AddressRequest extends BaseRequest<AddressContract.View> implements AddressContract.Presenter<AddressContract.View> {

    public static AddressRequest create() {
        return new AddressRequest();
    }


    @Override
    public void getAddress() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();

        Api.api_service.getAddress().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getAddressSuccess(result);
                });
    }

    @Override
    public void updateAddress(String name, String address, String phone, String province, String city, String area) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        UpdateAddressBean bean = new UpdateAddressBean();
        bean.address = address;
        bean.name = name;
        bean.phone = phone;
//        bean.provinces_cities = provinces_cities;
        bean.province = province;
        bean.city = city;
        bean.area = area;
        Api.api_service.updateAddress(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.updateAddressSuccess(result);
                });
    }
}
